'use strict';

/**
 * 用户管理控制器
 */
const Controller = require('egg').Controller;

class UserController extends Controller {
  /**
   * 创建用户
   * @return {Promise<void>}
   */
  async create() {
    const { ctx, service } = this;
    const userData = ctx.request.body;
    const result = await service.user.create(userData);
    ctx.success(result);
  }

  /**
   * 用户注册
   * @return {Promise<void>}
   */
  async register() {
    const { ctx, service } = this;
    const userData = ctx.request.body;

    // 检查用户名是否已存在
    const existingUser = await ctx.model.User.findOne({
      where: { username: userData.username },
    });

    if (existingUser) {
      ctx.throw(400, '用户名已存在');
    }

    // 设置默认状态为启用
    userData.status = true;

    const result = await service.user.create(userData);
    ctx.success(result, '注册成功');
  }

  /**
   * 查询用户列表
   * @return {Promise<void>}
   */
  async list() {
    const { ctx, service } = this;
    const { page = 1, pageSize = 10, ...query } = ctx.query;
    const result = await service.user.list(query, Number(page), Number(pageSize));
    ctx.success(result);
  }

  /**
   * 查询单个用户
   * @return {Promise<void>}
   */
  async show() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const result = await service.user.findById(id);
    ctx.success(result);
  }

  /**
   * 更新用户
   * @return {Promise<void>}
   */
  async update() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const userData = ctx.request.body;
    const result = await service.user.update(id, userData);
    ctx.success(result);
  }

  /**
   * 删除用户
   * @return {Promise<void>}
   */
  async destroy() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    await service.user.destroy(id);
    ctx.success(null, '删除成功');
  }

  /**
   * 批量删除用户
   * @return {Promise<void>}
   */
  async batchDestroy() {
    const { ctx, service } = this;
    const { ids } = ctx.request.body;
    await service.user.batchDestroy(ids);
    ctx.success(null, '批量删除成功');
  }

  /**
   * 重置密码
   * @return {Promise<void>}
   */
  async resetPassword() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { newPassword } = ctx.request.body;
    await service.user.resetPassword(id, newPassword);
    ctx.success(null, '密码重置成功');
  }

  /**
   * 更新用户状态
   * @return {Promise<void>}
   */
  async updateStatus() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { status } = ctx.request.body;
    await service.user.updateStatus(id, status);
    ctx.success(null, '状态更新成功');
  }


  /**
   * 用户登录
   * @return {Promise<void>}
   */
  async login() {
    const { ctx, service } = this;
    const loginData = ctx.request.body;
    const result = await service.user.login(loginData);
    ctx.success(result);
  }

  /**
   * 用户登出
   * @return {Promise<void>}
   */
  async logout() {
    const { ctx, service } = this;
    // 从token中获取用户ID
    const { id } = ctx.state.user;
    await service.user.logout(id);
    ctx.success(null, '登出成功');
  }

  /**
   * 修改密码
   * @return {Promise<void>}
   */
  async changePassword() {
    const { ctx, service } = this;
    // 从token中获取用户ID
    const { id } = ctx.state.user;
    const passwordData = ctx.request.body;
    await service.user.changePassword(id, passwordData);
    ctx.success(null, '密码修改成功');
  }
}
module.exports = UserController;
